package ch14;

import java.util.List;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class Fontshow extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    Checkboxes checkboxes1 = new Checkboxes();
    Comboboxes comboboxes1 = new Comboboxes();
    WordsPane wordsPane1 = new WordsPane();
    BorderPane mainpane = new BorderPane();

    @Override
    public void start(Stage primaryStage) throws Exception {
        mainpane.setBottom(checkboxes1);
        mainpane.setTop(comboboxes1);
        mainpane.setCenter(wordsPane1);
        Setfont();
        Scene scene = new Scene(mainpane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    void Setfont() {
        FontWeight bold = checkboxes1.checkbold.isSelected() ? FontWeight.BOLD : FontWeight.NORMAL;
        FontPosture italic = checkboxes1.checkitalic.isSelected() ? FontPosture.ITALIC : FontPosture.REGULAR;
        wordsPane1.label1
                .setFont(Font.font(comboboxes1.combofamily.getValue(), bold, italic, comboboxes1.combosize.getValue()));
    }

    class WordsPane extends StackPane {
        Label label1 = new Label("I can eat glass, it doesn't hurt me.\n我能吞下玻璃而不伤身体。");

        WordsPane() {
            getChildren().clear();
            getChildren().add(label1);
            setAlignment(Pos.CENTER);
        }
    }

    class Checkboxes extends HBox {
        CheckBox checkbold = new CheckBox("粗体");
        CheckBox checkitalic = new CheckBox("斜体");

        Checkboxes() {
            getChildren().clear();
            checkbold.setOnAction(e -> {
                Setfont();
            });
            checkitalic.setOnAction(e -> {
                Setfont();
            });
            getChildren().addAll(checkbold, checkitalic);
            setAlignment(Pos.CENTER);
        }
    }

    class Comboboxes extends HBox {
        Label label1 = new Label("字体:");
        Label label2 = new Label("大小:");
        ComboBox<String> combofamily = new ComboBox<>();
        ComboBox<Integer> combosize = new ComboBox<>();

        Comboboxes() {
            getChildren().clear();
            List<String> fontsfamily = Font.getFamilies();
            combofamily.getItems().addAll(fontsfamily);
            combofamily.setOnAction(e -> {
                Setfont();
            });
            combofamily.setValue("Arial");
            for (int i = 1; i <= 100; i++)
                combosize.getItems().add(i);
            combosize.setOnAction(e -> {
                Setfont();
            });
            combosize.setValue(50);
            getChildren().addAll(label1, combofamily, label2, combosize);
            setAlignment(Pos.CENTER);
        }
    }
}